package com.waymap.service.impl;

import com.github.pagehelper.PageHelper;
import com.waymap.common.vo.ItemInventorysDetail;
import com.waymap.common.vo.ItemsDetailVO;
import com.waymap.common.vo.ItemsVo;
import com.waymap.common.vo.SkuItemVo;
import com.waymap.domain.ItemInventorys;
import com.waymap.domain.Items;
import com.waymap.mapper.ItemsMapper;
import com.waymap.service.ItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class ItemServiceImpl extends BaseService<Items> implements ItemService {

    @Resource
    private ItemsMapper itemsMapper;

    /**
     * 产品分页查询
     *
     * @param param
     * @return
     */
    @Override
    public List<ItemsVo> getItemPage(Map<String, Object> param) {
        PageHelper.startPage((Integer) param.get("pageNum"), (Integer) param.get("pageSize"));
        List<ItemsVo> itemList = itemsMapper.getItemPage(param);
        return itemList;
    }

    @Override
    public Items getItemInfo(Map<String, Object> param) {
        return itemsMapper.getItemInfo(param);
    }

    /**
     * 查询产品sku总数
     *
     * @param param
     * @return
     */
    @Override
    public SkuItemVo getskuItemCount(Map<String, Object> param) {
        SkuItemVo skuItem = itemsMapper.getskuItemCount(param);
        return skuItem;
    }

    @Override
    public Items findInvDetailsBySku(Map<String, Object> param) {
        return itemsMapper.findInvDetailsBySku(param);
    }

    @Override
    public ItemsDetailVO findLotNumberByItemId(Map<String, Object> param) {
        /*查询商品*/
        Optional<Items> invDetailsBySku = Optional.ofNullable(itemsMapper.findInvDetailsBySku(param));
        ItemsDetailVO itemsDetailVO = new ItemsDetailVO();
        if (invDetailsBySku.isPresent()) {
            BeanUtils.copyProperties(invDetailsBySku.get(), itemsDetailVO);
            /*查询库存信息*/
            param.put("itemId",invDetailsBySku.get().getItemId());
            List<ItemInventorys> itemByBinAndItemId = itemsMapper.findItemByBinAndItemId(param);
            List<ItemInventorysDetail> itemInventorysDetails = itemByBinAndItemId.stream().map(e -> {
                ItemInventorysDetail itemInventorysDetail = new ItemInventorysDetail();
                BeanUtils.copyProperties(e, itemInventorysDetail);
                return itemInventorysDetail;
            }).collect(Collectors.toList());
            itemsDetailVO.setDetail(itemInventorysDetails);
            itemsDetailVO.setLotNumberCount(itemsMapper.lotNumberCount(param));
            itemsDetailVO.setBinCount(itemsMapper.binCount(param));
            return itemsDetailVO;
        }else {
            return null;
        }
    }


}
